using System;
using System.Timers;
using GameServer.Common.Physics;

namespace GameServer.ShootingGame.RenderableClasses
{

    public class MobileObject : GameObject
    {
        public double maxSpeed;
        //range(-1, 1)
        private double thrustX;
        //range(-1, 1)
        private double thrustY;
        private Velocity velocity;
        private double acceleration;
        private double deceleration;

        public MobileObject()
            : this(0, 0)
        {
        }

        public MobileObject(double xPos, double yPos)
            : this(xPos, yPos, 0)
        {
        }

        public MobileObject(double xPos, double yPos, double maxSpeed)
            : base(xPos, yPos)
        {
            thrustX = 0;
            thrustY = 0;
            velocity = new Velocity();
            this.maxSpeed = maxSpeed;
            acceleration = 0;
            deceleration = 0;

        }

        override public void Update()
        {
            if (IsExpired)
                return;
            base.Update();
            Vector2 velocityVector;
            
            UpdateVelocity();
            velocityVector = velocity.GetVector();
            xPosition += velocityVector.x;
            yPosition += velocityVector.y;
            UpdateThrust();
        }
        virtual protected void UpdateThrust()
        {

        }
        virtual protected void UpdateVelocity()
        {
            if (!(maxSpeed == 0))
            {
                velocity.direction = Math.Atan2(thrustY, thrustX) / Math.PI * 180.0;
                if (Math.Abs(thrustX) >= Math.Abs(thrustY))
                    Speed = Math.Abs(thrustX * maxSpeed);
                else
                    Speed = Math.Abs(thrustY * maxSpeed);
            }
        }

        public void ResetVelocity()
        {
            Speed = 0;
            velocity.direction = 0;
        }

        public double ThrustX
        {
            get
            {
                return thrustX;
            }
            set
            {
                if (value > 1)
                    thrustX = 1;
                else
                    if (value < -1)
                        thrustX = -1;
                    else
                        thrustX = value;
            }

        }


        public double ThrustY
        {
            get
            {
                return thrustY;
            }
            set
            {
                if (value > 1)
                    thrustY = 1;
                else
                    if (value < -1)
                        thrustY = -1;
                    else
                        thrustY = value;
            }

        }



        public double Speed
        {
            get
            {
                return velocity.speed;
            }
            set
            {

                if (maxSpeed == 0)
                    velocity.speed = value;
                else
                {
                    if (value > maxSpeed)
                        velocity.speed = maxSpeed;
                    else
                        if (value < 0)
                            velocity.speed = 0;
                        else
                            velocity.speed = value;
                }
            }
        }
        public double VelocityDirection
        {
            get
            {
                return velocity.direction;
            }
            set
            {
                if (value >= 360)
                {
                    value = value % 360;
                }
                else
                    if (value < 0)
                    {
                        while (value < 0)
                            value += 360;
                    }
                velocity.direction = value;
            }
        }
    }
}
